package com.gimi.cloud.jimi.commonService.common.impl;

import com.gimi.cloud.jimi.common.enums.common.RedisKeyEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.commonService.common.JmConfigService;
import com.gimi.cloud.jimi.commonService.common.JmInitService;
import com.gimi.cloud.jimi.commonService.service.CoreApiConfigService;
import com.gimi.cloud.jimi.commonService.util.BeanUtil;
import com.gimi.cloud.jimi.dao.dao.sys.SysConfigDao;
import com.gimi.cloud.jimi.dao.dao.userInfo.ApiConfigDao;
import com.gimi.cloud.jimi.dao.redis.IRedisService;
import com.gimi.cloud.jimi.dto.core.ApiConfigDTO;
import com.gimi.cloud.jimi.dto.sys.SysConfigDTO;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shazhiwei on 2018/1/24.
 */
@Service
public class JmInitServiceImpl implements JmInitService {

    private final SysConfigDao sysConfigDao;
    private final IRedisService iRedisService;
    private final ApiConfigDao apiConfigDao;
    private final JmConfigService jmConfigService;

    private static final Logger logger = LoggerFactory.getLogger(JmInitServiceImpl.class);

    @Autowired
    public JmInitServiceImpl(SysConfigDao sysConfigDao,IRedisService iRedisService,ApiConfigDao apiConfigDao,JmConfigService jmConfigService){
        this.sysConfigDao = sysConfigDao;
        this.iRedisService = iRedisService;
        this.apiConfigDao = apiConfigDao;
        this.jmConfigService = jmConfigService;
    }

    @Override
    public void init(ServletContext context) {

    }

    @Override
    public void initSysConfig() {
        List<SysConfigDTO> configDTOList = sysConfigDao.getUseableConfigList();
        if(configDTOList != null && configDTOList.size() > 0){
            for(SysConfigDTO configDTO : configDTOList){
                String pre = RedisKeyEnum.JIMI_CONFIG_PRE.getKey();
                String cfKey = configDTO.getCfKey();
                String cfValue = configDTO.getCfValue();
                iRedisService.set(pre+cfKey,cfValue);
            }
            logger.info("===>初始化sys_config完成，size = " + configDTOList.size());
        }else{
            logger.info("===>sys_config表中没有需要初始化的数据");
        }
    }

    @Override
    public void initSysApiConfig() {
        //API配置文件
        CoreApiConfigService apiConfigService = (CoreApiConfigService) BeanUtil.getBean("coreApiConfigService");
        List<ApiConfigDTO> apiConfigs = apiConfigDao.getApiConfigList();
        if(apiConfigs != null && apiConfigs.size() > 0){
            Map<String, ApiConfigDTO> apiConfigMap = new HashMap<String, ApiConfigDTO>();
            for(ApiConfigDTO apiConfigDTO : apiConfigs){
                if (StringUtil.isNotBlank(apiConfigDTO.getNid())) {
                    apiConfigMap.put(apiConfigDTO.getNid(), apiConfigDTO);
                }
            }
            iRedisService.set(RedisKeyEnum.JIMI_API_CONFIG_PRE.getKey(), JsonUtil.toJson(apiConfigMap));
            logger.info("===>初始化sys_api_config完成，size = " + apiConfigMap.size());
        }else {
            logger.info("===>sys_api_config表中没有需要初始化的数据");
        }
        //TODO:未完成
        //Global.API_CONFIG = apiConfigMap;
    }

    @Override
    public void initOssClient() {
        String ossAccountNumber = null;
        try{
            ossAccountNumber = jmConfigService.getValueByKey("ali_oss_account_number");
            Map<String,String> accountMap = JsonUtil.jsonToMap(ossAccountNumber);
            String endpoint = MapUtils.getString(accountMap,"endpoint");
            String accessKeyId = MapUtils.getString(accountMap,"accessKeyId");
            String accessSecret = MapUtils.getString(accountMap,"accessSecret");
            String bucketName = MapUtils.getString(accountMap,"bucketName");
            AliOSSUtil.init(endpoint,accessKeyId,accessSecret,bucketName);
        }catch (Exception e){
            logger.error("初始化oss客户端失败！将导致图片上传功能失效，具体参数为：");
            logger.info(ossAccountNumber);
            e.printStackTrace();
        }
    }

}
